package com.yykj.backstage.all.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yykj.backstage.all.dto.AdminDTO;
import com.yykj.backstage.all.dto.AdminLoginInfo;
import com.yykj.backstage.all.dto.AdminPasswordDTO;
import com.yykj.backstage.all.form.AdminEditForm;
import com.yykj.backstage.all.form.LoginForm;
import com.yykj.backstage.all.service.AdminService;
import com.yykj.common.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/backstage/all")
@Api(tags = "总后台账号相关接口")
@SuppressWarnings(value = "unchecked")
public class BackstageAccountController {

    @PostMapping("/admin/login")
    @ApiOperation("登录接口")
    public ResultVO<AdminLoginInfo> login(@RequestBody LoginForm loginForm){
        ResultVO<AdminLoginInfo> success = ResultVO.success(adminService.login(loginForm));
        success.setMsg("");
        return success;
    }

    @GetMapping("/admin/login/info/{token}")
    @ApiOperation("获取登录信息")
    public ResultVO<AdminLoginInfo> loginInfo(@PathVariable String token){
        return ResultVO.success(adminService.loginInfo(token));
    }


    @GetMapping("/admin/logout")
    @ApiOperation("登出接口")
    public ResultVO logout(){
        adminService.logout();
        return ResultVO.success();
    }

    @PostMapping("/admin/add")
    @ApiOperation("管理员--创建")
    @ApiOperationSupport(ignoreParameters = {"id"})
    public ResultVO<AdminDTO> addAdmin(@RequestBody AdminEditForm adminEditForm){
        return ResultVO.success(adminService.editAdmin(adminEditForm));
    }

    @PutMapping("/admin/edit")
    @ApiOperation("管理员--修改")
    public ResultVO<AdminDTO> editAdmin(@RequestBody AdminEditForm adminEditForm){
        return ResultVO.success(adminService.editAdmin(adminEditForm));
    }

    @DeleteMapping("/admin/delete/{adminId}")
    @ApiOperation("管理员--删除")
    public ResultVO<AdminDTO> deleteAdmin(@PathVariable Integer adminId){
        adminService.deleteAdmin(adminId);
        return ResultVO.success();
    }

    @GetMapping("/admin/list")
    @ApiOperation("管理员--列表")
    public ResultVO<List<AdminDTO>> getAdminList(){
        return ResultVO.success(adminService.getAdminList());
    }

    @ApiOperation("管理员--修改密码")
    @PostMapping("/change/admin/password")
    public ResultVO changeAdminPassword(@RequestBody AdminPasswordDTO adminPasswordDTO){
        adminService.changeAdminPassword(adminPasswordDTO);
        return ResultVO.success();
    }


    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }
}
